#include <types.h>
#include <osal/osal.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <api/libosd/osd_lib.h>

#include "osdobjs_def.h"

#include "osd_config.h"
#include "string.id"
#include "images.id"

#include "menus_root.h"
#include "win_com.h"


/*******************************************************************************
*	Objects definition
*******************************************************************************/
extern TEXT_FIELD 	g_win_PALNTSC;

static VACTION palntsc_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT palntsc_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

#define WIN_SH_IDX	 WSTL_POP_WIN_01_8BIT// WSTL_TEXT_03

#define	W_L     400
#define	W_T     66
#define	W_W     80
#define	W_H     40

#define LDEF_PALNTSC(varTxt,l,t,w,h,resID)		\
    DEF_TEXTFIELD(varTxt,NULL,NULL,C_ATTR_ACTIVE,0, \
    1,1,1,1,1, l,t,w,h, WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX,   \
    palntsc_keymap,palntsc_callback,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 0,0,resID,NULL)


LDEF_PALNTSC(g_win_PALNTSC,W_L, W_T, W_W, W_H,0)

/*******************************************************************************
*	Local vriable & function declare
*******************************************************************************/


#define TVMODE_UI_TIMER_TIME		2000
#define TVMODE_UI_TIMER_NAME 		"PAL"
static ID tvmode_UI_timer_id = OSAL_INVALID_ID;

static void win_tvmode_UI_handler(UINT nouse);


/*******************************************************************************
*	key mapping and event callback definition
*******************************************************************************/
static VACTION palntsc_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;

	switch(key)
	{
	case V_KEY_PAL_NTSC:
		act = VACT_ENTER;
		break;
	default:
		act = VACT_CLOSE;
	}

	return act;
}

static PRESULT palntsc_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	VACTION unact;
	SYSTEM_DATA* sys_data;
	UINT8 tv_out;
	TEXT_FIELD *txt;
	UINT16 str_id;

	txt = &g_win_PALNTSC;

	sys_data = sys_data_get();

	switch(event)
	{
	case EVN_PRE_OPEN:

		tv_out = api_video_get_tvout();
		if(tv_out != TV_MODE_PAL && tv_out != TV_MODE_NTSC358)
		{
			tv_out = TV_MODE_PAL;
			api_video_set_tvout(sys_data->avset.tv_mode = tv_out);
		}
		str_id = (tv_out == TV_MODE_PAL)? RS_SYSTEM_TV_SYSTEM_PAL : RS_SYSTEM_TV_SYSTEM_NTSC;
		OSD_SetTextFieldContent(txt, STRING_ID, (UINT32)str_id);
		break;
	case EVN_POST_OPEN:
		api_stop_timer(&tvmode_UI_timer_id);
		tvmode_UI_timer_id = api_start_timer(TVMODE_UI_TIMER_NAME, TVMODE_UI_TIMER_TIME, win_tvmode_UI_handler);		
		break;
	case EVN_PRE_CLOSE:
		break;
	case EVN_POST_CLOSE:
		break;
	case EVN_UNKNOWN_ACTION:
		unact = (VACTION)(param1>>16);
		if(unact == VACT_ENTER)
		{
			tv_out = api_video_get_tvout();
			tv_out = (tv_out == TV_MODE_PAL)? TV_MODE_NTSC358 : TV_MODE_PAL;
			str_id = (tv_out == TV_MODE_PAL)? RS_SYSTEM_TV_SYSTEM_PAL : RS_SYSTEM_TV_SYSTEM_NTSC;
			OSD_SetTextFieldContent(txt, STRING_ID, (UINT32)str_id);
			api_video_set_tvout(sys_data->avset.tv_mode = tv_out);

			OSD_TrackObject( (POBJECT_HEAD)txt, C_UPDATE_ALL);			
			ret = PROC_LOOP;		

			api_stop_timer(&tvmode_UI_timer_id);
			tvmode_UI_timer_id = api_start_timer(TVMODE_UI_TIMER_NAME, TVMODE_UI_TIMER_TIME, win_tvmode_UI_handler);

		}
		break;
	}

	return ret;
	
}


static void win_tvmode_UI_handler(UINT nouse)
{
	if(tvmode_UI_timer_id != OSAL_INVALID_ID)
	{
		tvmode_UI_timer_id = OSAL_INVALID_ID;
		ap_send_msg(CTRL_MSG_SUBTYPE_CMD_EXIT_ROOT, (UINT32)&g_win_PALNTSC,FALSE);
	}
}


